package com.dj.shop.controller;


import com.dj.shop.model.Goods;
import com.dj.shop.service.GoodsService;
import com.dj.shop.valid.Insert;
import com.dj.shop.vo.GoodsVo;
import com.dj.shop.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description:
 *      线上商城
 * @Author: zhaoyu
 * @CreateDate: 2018/2/19 21:48
 * @UpdateUser:
 * @UpdateDate:
 * @UpdateRemark:
 */

@Controller
@Validated
@RequestMapping("/shop")
public class ShopController extends BaseController {

    /**
     * 日志
     */
    private final Logger logger = LoggerFactory.getLogger(ShopController.class);

    @Autowired
    private GoodsService goodsService;

    /**
     * 增加商品信息
     * @param goodsVo
     * @return

    @RequestMapping(value = "/addGoods", method = RequestMethod.POST)
    public @ResponseBody
    Result addGoods(@Validated({Insert.class}) GoodsVo goodsVo, BindingResult bindingResult){

        // 入参校验
        String validResult = this.getValidResult(bindingResult);
        if ( validResult!= null){
            return Result.error(validResult);
        }

        // 视图模型与持久层模型转换
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsVo,goods);
        GoodsClassification gc = new GoodsClassification();
   //     gc.setClassiId(goodsVo.getClassId());
   //     goods.setGoodsClassification(gc);
        // 调用添加商品服务
        goodsService.addGoods(goods);

        //逻辑处理
        Result result = null;
        try {
        result = Result.success();
    }catch (Exception e) {
        result = Result.error();
        logger.error("操作失败：{}", e);
    }

        return result;
}

     */
    @PostMapping(value = "/queryGoodsList")
    public @ResponseBody Result queryGoodsList(@NotNull(message = "classiId 不能为空")  String classiId){
        Map<String,Object> parMap = new HashMap<String,Object>();
        parMap.put("classiId",classiId);

        List<Goods> goodsList = null;
        Result result = null;
        try {
      //      goodsList = goodsService.queryGoodsList(parMap);
            result = Result.success(goodsList);
        }catch (Exception e) {
            result = Result.error();
            logger.error("操作失败：{}", e);
        }

        return result;
    }
}
